public class InterviewerPicker{
        private Boolean firstTime {
            get{
                if (firstTime == null) firstTime = true;
                return firstTime;
            }
            set;    
        }
        public String searchText { get; set; }
        public Position__c position {
            get{
                position = [select id,name from position__c where id=:System.currentPageReference().getParameters().get('id')];
                return position;
            }
            set;    
        }
        public List<User> results {
            get{
                if (firstTime) {
                    firstTime = false;
                    String d = [select id, Department__c from Position__c where id =:System.currentPageReference().getParameters().get('id')].Department__c;
                    results = [select id, name, title, interview_dept__c from User where interview_dept__c = :d order by name];
                    if (results.size() > 0){
                            selected = new Interviewer__c();
                            selected.Employee__c = results[0].Id;                              
                    }                   
                }
                return results;
            }
            set;    
        }
        public String applyInterviewerId {
            get;
            set{
                applyInterviewerId = value; 
            }
        }
        
        public Interviewer__c functional {
            get{
                if ([Select count() From Interviewer__c i where i.Position__c=:System.currentPageReference().getParameters().get('id') 
                                    and i.Role__c = 'Functional'] > 0){
                functional = [Select i.Id,i.Name, i.Position__c,i.Employee__c,i.Employee__r.Name,i.Role__c From
                            Interviewer__c i where i.Position__c=:System.currentPageReference().getParameters().get('id') 
                            and i.Role__c = 'Functional' order by i.Employee__r.Name LIMIT 1];
                } else { functional = new Interviewer__c(); }
                return functional;  
            }   
            set;
        }
        
        public Interviewer__c technical{
            get{
                if ([Select count() From Interviewer__c i where i.Position__c=:System.currentPageReference().getParameters().get('id') 
                                    and i.Role__c = 'Technical'] > 0) {
                        technical = [Select i.Id,i.Name, i.Position__c,i.Employee__c,i.Employee__r.Name,i.Role__c From
                                    Interviewer__c i where i.Position__c=:System.currentPageReference().getParameters().get('id') 
                                    and i.Role__c = 'Technical' order by i.Employee__r.Name LIMIT 1];
                } else { technical = new Interviewer__c(); }
                return technical;
            }
            set;    
        }
        
        public Interviewer__c general {
            get{
                if ([Select count() From Interviewer__c i where i.Position__c=:System.currentPageReference().getParameters().get('id') 
                                    and i.Role__c = 'General'] > 0){
                        general = [Select i.Id,i.Name, i.Position__c,i.Employee__c,i.Employee__r.Name,i.Role__c From
                                Interviewer__c i where i.Position__c=:System.currentPageReference().getParameters().get('id') 
                                and i.Role__c = 'General' order by i.Employee__r.Name LIMIT 1];
                } else { general = new Interviewer__c(); }          
                return general;
            }
            set;    
        }
        
        public Interviewer__c managerial {
            get {
                if ([Select count() From Interviewer__c i where i.Position__c=:System.currentPageReference().getParameters().get('id') 
                                    and i.Role__c = 'Managerial'] > 0){
                    managerial = [Select i.Id,i.Name, i.Position__c,i.Employee__c,i.Employee__r.Name,i.Role__c From
                                    Interviewer__c i where i.Position__c=:System.currentPageReference().getParameters().get('id') 
                                    and i.Role__c = 'Managerial' order by i.Employee__r.Name LIMIT 1];
                } else { managerial = new Interviewer__c(); }
                return managerial;  
            }   
            set;
        }
        
        public Interviewer__c selected {
            get{
                if (firstTime) {
                    String d = [select id, Department__c from Position__c where id =:System.currentPageReference().getParameters().get('id')].Department__c;
                    results = [select id, title, name, interview_dept__c from User where interview_dept__c = :d order by name];
                    if (results.size() > 0){
                            selected = new Interviewer__c();
                            selected.Employee__c = results[0].Id;
                            selected.Role__c = '';     
                            applyInterviewerId = results[0].Id;                           
                    }
                    // firstTime = false;               
                } else {         
                    if (selected == null) {   
                        selected = new Interviewer__c(Role__c = role, Position__c = position.Id);   
                        if (applyInterviewerId != null){                      
                            selected.Employee__c = applyInterviewerId;      
                        }
                        if (this.role != null){                      
                            selected.Role__c = this.role;      
                        }
                    }
                }
                return selected;
            }
            set;    
        }        
        
        public String role{
            get{
                if (role == null) {role = 'General';}
                return role;
            }
            set;
        }
        
        public PageReference changeRole(){
            // if the role changes we want to clone the currently selected interviewer record
            //  and make a new copy so we can insert it if necessary.  if we don't clone it
            //  then the selected interviewer may already have an id value which would
            //  prevent it from being inserted
            selected = selected.clone(false,true);
            System.debug('selected=' + selected);
            return null;
        }
        
        public PageReference doSearch() {
                        results = (List<User>)[FIND :searchText IN ALL FIELDS RETURNING User(Id, Name, title, Interview_Dept__c)] [0];
                        // firstTime = false;
                        return null;
                }
        
        public PageReference changeSelected() {
            // set the new employee id of the interviewer, then clone the record to drop the id
            selected.Employee__c = applyInterviewerId;
            selected = selected.clone(false,true);
            return null;
        }
    
        public PageReference doUpdateInterviewers() {
                Boolean roleMatchFound = false;     
                Boolean roleSelected = true;    
                Interviewer__c[] deletes = new Interviewer__c[0];
                
                if (selected.Role__c == 'Functional') {
                        if (functional.id != null) { deletes.add(functional); roleMatchFound = true; }
                } else if (selected.Role__c == 'Technical') {
                        if (technical.id != null) { deletes.add(technical); roleMatchFound = true; }
                } else if (selected.Role__c == 'General') {
                        if (general.id != null) { deletes.add(general); roleMatchFound = true; }
                } else if (selected.Role__c == 'Managerial') {
                        if (managerial.id != null) { deletes.add(managerial); roleMatchFound = true; }
                } else if (selected.Role__c == null) {
                        roleSelected = false;
                }
                
                System.debug('******************************' + selected);
                
                if (roleSelected) {
                        if (roleMatchFound) {      
                                System.debug(deletes);          
                                delete deletes;      
                        }
                        
                        if (selected.Position__c == null) {
                                selected.Position__c = position.Id;
                        }
                        // make sure the record doesn't already have an id, if it does
                        // then clone it and insert the clone.
                        //  -- this can happen when adding the same person to be the interviewer
                        //      for multiple roles
                        if (selected.id != null) selected = selected.clone(false,true);
                        insert selected;
                }
            return null;
        }

        public PageReference cancel() {
                String sUrl = '/' + position.Id;
                PageReference posPageRef = new PageReference(sUrl);
            return posPageRef;
        }
}